package com.tledu.springboot04.controller;

import com.tledu.springboot04.constants.CommonConstants;
import com.tledu.springboot04.core.model.AjaxResult;
import com.tledu.springboot04.domain.User;
import com.tledu.springboot04.service.IUserService;
import lombok.AllArgsConstructor;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@AllArgsConstructor
public class LoginController {

    private final IUserService userService;

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @PostMapping("/login")
    @ResponseBody
    public AjaxResult<User> loginAjax(String username, String password, HttpServletRequest req) {
        User user = userService.login(username, password);
        req.getSession().setAttribute(CommonConstants.LOGIN_USER_KEY, user);
        return new AjaxResult<>(user);
    }
}
